package libcommonuse11.observer;

import java.util.Observable;
import java.util.Observer;
class House extends Observable
{
	private float price;
	
	

	public House(float price)
	{
		super();
		this.setPrice(price);
	}

	public float getPrice()
	{
		return price;
	}

	public void setPrice(float price)
	{
		//设置观察点
		super.setChanged();
		super.notifyObservers(price);
		this.price = price;
	}

	@Override
	public String toString()
	{
		return "House [price=" + price + "]";
	}
	
}

class ToBuyHouseObserver implements Observer
{
	private String name = null;
	
	 public ToBuyHouseObserver(String name)
	{
		super();
		this.name = name;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public void update(Observable o,
            Object arg)
	{
		 if(arg instanceof Float)
		 {
			 System.out.println("当前最新价格是："+((Float)arg).floatValue());
		 }
	}
	
}


public class ObserverDemo
{

	public static void main(String[] args)
	{
		House h = new House(555555f);
		ToBuyHouseObserver toBuyHou1 = new ToBuyHouseObserver("路人甲");
		ToBuyHouseObserver toBuyHou2 = new ToBuyHouseObserver("路人已");
		ToBuyHouseObserver toBuyHou3 = new ToBuyHouseObserver("路人丙");
		h.addObserver(toBuyHou1);
		h.addObserver(toBuyHou2);
		h.addObserver(toBuyHou3);
		System.out.println("当前价格是："+h);
		h.setPrice(88888f);
		h.notifyObservers();
	}

}
